package com.dayuanit.dy16.atm.boatm.controller;

import com.dayuanit.dy16.atm.boatm.dto.ResponseDTO;
import com.dayuanit.dy16.atm.boatm.entity.BoUser;
import com.dayuanit.dy16.atm.boatm.service.IBoUserService;
import com.dayuanit.dy16.atm.boatm.vo.PerimissionRequestVO;
import com.dayuanit.dy16.atm.boatm.vo.UserRoleAllocationRequstVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户controller
 *
 */
@RestController
public class UserController extends BaseController {

    @Autowired
    private IBoUserService boUserService;

    @RequestMapping("/user/signUp")
    public ResponseDTO signUp(String username, String password) {
        boUserService.signUp(username, password);
        return ResponseDTO.success();
    }

    @RequestMapping("/user/createRole")
    public ResponseDTO createRole(String roleName, String roleValue) {
        boUserService.createRole(roleName, roleValue);
        return ResponseDTO.success();
    }

    /**
     * 为用户分配角色
     * @return
     */
    @RequestMapping("/user/userRoleAllocation")
    public ResponseDTO userRoleAllocation(String username, @RequestParam(value = "roleIds[]", required = false) List<Long> roleIds) {
        UserRoleAllocationRequstVO userRoleAllocationRequstVO = new UserRoleAllocationRequstVO();
        userRoleAllocationRequstVO.setRoleIds(roleIds);
        userRoleAllocationRequstVO.setUsername(username);
        boUserService.userRole(userRoleAllocationRequstVO);
        return ResponseDTO.success();
    }

    /**
     * 删除用户对应的角色
     * @param userId
     * @param roleId
     * @return
     */
    @RequestMapping("/user/delUserRole")
    public ResponseDTO delUserRole(Long userId, Long roleId) {
        boUserService.delUserRole(userId, roleId);
        return ResponseDTO.success();
    }

    @RequestMapping(value = "/user/loadRoles")
    public ResponseDTO loadRoles() {
        return ResponseDTO.success(boUserService.loadRole());
    }

    @RequestMapping("/user/loadUserRoles")
    public ResponseDTO loadUserRoles(String username) {
        return ResponseDTO.success(boUserService.loadUserRoles(username));
    }

    @RequestMapping("/user/createPermission")
    public ResponseDTO createPermission(PerimissionRequestVO perimissionRequestVO) {
        boUserService.createPerimission(perimissionRequestVO);
        return ResponseDTO.success();
    }

    @RequestMapping("/user/loadFirstMenu")
    public ResponseDTO loadFirstMenu() {
        return ResponseDTO.success(boUserService.listFirstPermission());
    }


    @RequestMapping("/user/loadMenu")
    public ResponseDTO loadMenu() {
        return ResponseDTO.success(boUserService.loadMenu());
    }

    @RequestMapping("/user/loadUnMenu")
    public ResponseDTO loadUnMenu() {
        return ResponseDTO.success(boUserService.loadUnMenu());
    }

    @RequestMapping("/user/rolePerimissionAllocation")
    public ResponseDTO rolePerimissionAllocation(Long roleId,
                                                 @RequestParam(value = "menuIds[]", required = false) List<Long> menuIds,
                                                 @RequestParam(value = "unMenuIds[]", required = false) List<Long> unMenuIds) {
        boUserService.rolePerimissionAllocation(roleId, menuIds, unMenuIds);
        return ResponseDTO.success();
    }

    @RequestMapping("/user/queryUserPermission")
    public ResponseDTO queryUserPermission(Long roleId) {
        return ResponseDTO.success(boUserService.queryUserPermission(roleId));
    }

    @RequestMapping("/user/loadUserMenu")
    public ResponseDTO loadUserMenu() {
        return ResponseDTO.success(boUserService.loadUserMenu(getUserId()));
    }


    @RequestMapping("/user/login")
    public ResponseDTO login(String username, String password) {
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken();
        usernamePasswordToken.setUsername(username);
        usernamePasswordToken.setPassword(password.toCharArray());

        Subject subject = SecurityUtils.getSubject();

        subject.login(usernamePasswordToken);

        BoUser user = boUserService.getUser(username);

        Session session = subject.getSession();
        session.setAttribute("loginUser", user);

        return ResponseDTO.success();
    }

}
